package com.stockz.ui.swing.watchlist.widget;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JOptionPane;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.stockz.core.model.Scrip;
import com.stockz.ui.swing.charting.view.StockChartView;
import com.stockz.ui.swing.core.MainFrame;
import com.stockz.ui.swing.widget.JMenuItem;
import com.stockz.watchlist.manager.WatchListManager;
import com.stockz.watchlist.model.WatchList;

@Component
public class ShowWatchListActionListener implements ActionListener {

	@Autowired
	private WatchListManager watchListManager;
	
	
	@Override
	public void actionPerformed(ActionEvent e) {
		JMenuItem item = (JMenuItem)e.getSource();
		WatchList watchList = (WatchList)item.getPayload();
		
		List<Scrip> scripList = watchListManager.getScripList(watchList.getId());
		if(scripList == null || scripList.isEmpty()){
			JOptionPane.showMessageDialog(MainFrame.getInstance(),
					"There are no Scrips added to mentioned WatchList",
				    "Empty WatchList",
				    JOptionPane.ERROR_MESSAGE);
		}else{
			MainFrame.getInstance().add(new StockChartView(scripList));
		}
	}

}
